package client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;

//za metodu getTasks()
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Sasa
 */
public class Requests {

    Socket socket;
    BufferedWriter out;
    BufferedReader in;

    public Requests(Socket socket) throws Exception {
	this.socket = socket;

	/* writer & reader */
	this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
	this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
    }

    public Integer checkLogin(String username, String password) throws Exception {
	String msg = "LOGIN#" + username + "#" + hashPassword(password);
	sendMessage(msg);

	return getUserId();
    }

    public void sendMessage(String msg) throws Exception {
	if (this.socket.isClosed()) {
	    this.socket = new Socket("localhost", 9876);
	}
	out.write(msg);
	out.newLine();
	out.flush();
    }

    public Integer getUserId() throws Exception {
	return Integer.parseInt(in.readLine());
    }

    public Boolean getResponse() throws Exception {
	return (in.readLine().equals("OK")) ? true : false;
    }

    private static String hashPassword(String password) throws Exception {
	String hashword = null;
	MessageDigest md5 = MessageDigest.getInstance("MD5");
	md5.update(password.getBytes());
	BigInteger hash = new BigInteger(1, md5.digest());
	hashword = hash.toString(16);
	return hashword;
    }

    public List<Task> getTasks(Integer memberId) throws Exception {
	String filename = "xmls/tasks-" + memberId + ".xml";
	sendMessage("TASKS#" + memberId);
	getXML(filename);

	BufferedReader in = new BufferedReader(new FileReader(filename));
	myXML xmlRoot = new myXML((BufferedReader) in);

	List<Task> tasks = new ArrayList<Task>();

	for (Integer i = 0; i < xmlRoot.size(); i++) {
	    myXML xmlTask = xmlRoot.getElement(i);

	    Task task = new Task();
	    task.id = Integer.parseInt(xmlTask.findElement("id").getValue());
	    task.memberId = Integer.parseInt(xmlTask.findElement("memberId").getValue());
	    task.assigment = xmlTask.findElement("assigment").getValue();

	    tasks.add(task);
	}
	return tasks;
    }

    public List<Member> getMembers() throws Exception {

	String filename = "xmls/members.xml";
	sendMessage("MEMBERS");
	getXML(filename);

	BufferedReader in = new BufferedReader(new FileReader(filename));
	myXML xmlRoot = new myXML((BufferedReader) in);

	List<Member> members = new ArrayList<Member>();

	for (Integer i = 0; i < xmlRoot.size(); i++) {
	    myXML xmlMember = xmlRoot.getElement(i);

	    Member member = new Member();
	    member.mId = Integer.parseInt(xmlMember.findElement("id").getValue());
	    member.mName = xmlMember.findElement("username").getValue();

	    members.add(member);
	}
	return members;
    }


    public void completeTask(Integer taskId) throws Exception {
	sendMessage("COMPLETE#" + taskId);
    }

    public String getXML(String filename) throws Exception {

	String line;
	StringBuilder xml = new StringBuilder();
	BufferedWriter bw = new BufferedWriter(new FileWriter(filename));

	while (!(line = in.readLine()).equals("")) {
	    xml.append(line);
	    xml.append("\n");
	}
	bw.write(xml.toString());
	bw.close();


	return filename;
    }

    public void newMember(String username, String password) throws Exception {
	sendMessage("NEWMEMBER#"+username+"#"+hashPassword(password));
    }
    public void newTask(Integer id, String assgment) throws Exception {
	sendMessage("NEWTASK#"+id+"#"+assgment);
    }
}
